package com.example.yupao_project.component;

import com.example.yupao_project.utils.AppVar;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * 拦截规则（未登录拦截）
 * @author: 杰
 * @date: 2024/5/1 10:29
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 未登录拦截（在处理器执行之前被调用的拦截规则）
     * @param request 请求
     * @param response 响应
     * @param handler 处理器
     * @return true/false
     * @throws Exception 异常信息
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断session是否存在
        HttpSession session = request.getSession(false);
        return session != null && session.getAttribute(AppVar.SESSION_KEY_USERINFO) != null;//不存在则进行拦截
    }
}
